﻿using System;
using System.Collections.Generic;
using System.Linq;
using Sumen.Model.Model;

namespace Sumen.Business.Presenter.Actor
{
    public class CustomerPresenter 
    {


        public int Update(object pObjectValue, object pId)
        {
            using (sumenEntities objEntities = new sumenEntities())
            {
                string user = pId + string.Empty;
                customer newCustomer = pObjectValue as customer;

                customer oldCustomer = objEntities.customers.FirstOrDefault(p => p.Cus_Username == user);
                oldCustomer.Cus_Username = newCustomer.Cus_Username;
                if (newCustomer.Cus_Password != "")
                {
                    newCustomer.Cus_Password = ClsPublic.EncryptSHA512(newCustomer.Cus_Password);
                    oldCustomer.Cus_Password = newCustomer.Cus_Password;
                }

                oldCustomer.Cus_CUSTOMER_TYPE_ID = newCustomer.Cus_CUSTOMER_TYPE_ID;
                oldCustomer.Cus_FullName = newCustomer.Cus_FullName;
                oldCustomer.Cus_Phone = newCustomer.Cus_Phone;
                oldCustomer.Cus_Address = newCustomer.Cus_Address;
                oldCustomer.Cus_District = newCustomer.Cus_District;
                oldCustomer.Cus_City = newCustomer.Cus_City;
                oldCustomer.Cus_Contact = newCustomer.Cus_Contact;
                oldCustomer.Cus_Email = newCustomer.Cus_Email;
                oldCustomer.Cus_Actived = newCustomer.Cus_Actived;
                oldCustomer.Cus_Facebook = newCustomer.Cus_Facebook;
                return objEntities.SaveChanges();
            }

        }

        public int Add(object pObjectValue)
        {
            using (sumenEntities objEntities = new sumenEntities())
            {
                customer newCustomer = pObjectValue as customer;
                newCustomer.Cus_Password = ClsPublic.EncryptSHA512(newCustomer.Cus_Password);
                objEntities.customers.Add(newCustomer);
                return objEntities.SaveChanges();
            }
        }

        public int Delete(object pObjectValue)
        {
            using (sumenEntities objEntities = new sumenEntities())
            {
                string user = pObjectValue + string.Empty;
                objEntities.customers.Remove(objEntities.customers.FirstOrDefault(p => p.Cus_Username == user));
                return objEntities.SaveChanges();
            }
        }

        public ICollection<customer> GetAll()
        {
            using (sumenEntities objEntities = new sumenEntities())
            {
                return objEntities.customers.ToList();
            }
        }

        public customer GetById(object pId)
        {
            using (sumenEntities objEntities = new sumenEntities())
            {
                objEntities.Configuration.LazyLoadingEnabled = false;
                objEntities.Configuration.ProxyCreationEnabled = false;
                string user = pId.ToString();
                customer cus = objEntities.customers.FirstOrDefault(p => p.Cus_Username == user);
                if (cus!=null)
                {
                    objEntities.Entry(cus).Reference(p => p.customer_type).Load();
                    objEntities.Entry(cus.customer_type).Reference(p => p.price_type).Load();
                }
                return cus;
            }
        }


        /// <summary>
        /// Get customer list with Active or not
        /// </summary>
        /// <param name="pValue"></param>
        /// <returns></returns>
        public ICollection<customer> GetByKey(object pObect)
        {
            using (sumenEntities objEntities = new sumenEntities())
            {
                string active = ClsPublic.SYSTEM_DISPLAY_ACTIVED;
                if (active == "1")
                {
                    return GetAll();
                }
                else
                {
                    return objEntities.customers.Where(p => p.Cus_Actived == 1).ToList();
                }
            }
        }


        public long GetMaxSortedByCusType(int pCusTypeId)
        {
            using (sumenEntities objSumenEntities = new sumenEntities())
            {
                long sorted = objSumenEntities.customers.Where(p => p.Cus_CUSTOMER_TYPE_ID == pCusTypeId).Max(p => p.Cus_Sorted) ?? 0;
                return sorted + 1;
            }
        }





    }
}
